//
//  ZBGiftOperationManager.m
//  送礼
//
//  Created by 华惠友 on 2017/9/20.
//  Copyright © 2017年 华惠友. All rights reserved.
//

#import "ZBGiftOperationManager.h"
#import "ZBGiftOperation.h"

@interface ZBGiftOperationManager () {
    NSString *_oldGiftKey;
}

@property (nonatomic, strong) NSOperationQueue *queue1;
@property (nonatomic, strong) NSOperationQueue *queue2;

@property (nonatomic, strong) NSMutableDictionary *oprationCache;
@property (nonatomic, strong) NSMutableDictionary *userInfoCache;

@end

@implementation ZBGiftOperationManager

+ (instancetype)shareManager {
    static ZBGiftOperationManager *manager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[ZBGiftOperationManager alloc] init];
        
    });
    return manager;
}


- (void)animationWithGiftIndex:(NSInteger)giftIndex
                     giftModel:(ZBGiftModel *)giftModel
                 finishedBlock:(GiftFinishedBlock)finishedBlock{
    
    NSString *key = [NSString stringWithFormat:@"%lu%@",(unsigned long)giftModel.userID, giftModel.giftName];
    
    
    NSLog(@"%@",key);
    if (self.userInfoCache) {
        NSLog(@"----%@",self.userInfoCache);
    }
    
    if ([self.userInfoCache objectForKey:key]) { // 有用户缓存信息(缓存对应用户的礼物数)
        if ([self.oprationCache objectForKey:@(giftModel.userID)]) {
            // 操作缓存中存在, 则不创建操作  直接动画
            ZBGiftOperation *op = [self.oprationCache objectForKey:@(giftModel.userID)];
//            op.giftModel = giftModel;
            op.presentView.headImageView.image = [UIImage imageNamed:giftModel.headName];
            op.presentView.giftImageView.image = [UIImage imageNamed:giftModel.giftImageName];
            op.presentView.nameLabel.text = giftModel.name;
            op.presentView.giftLabel.text = [NSString stringWithFormat:@"送了%@",giftModel.giftName];
            // 从用户信息缓存中将礼物数取出来 直接+1 并重新缓存礼物数量  手动执行动画
            op.presentView.giftCount = [[self.userInfoCache objectForKey:key] integerValue] + 1;
            [self.userInfoCache setObject:@(op.presentView.giftCount) forKey:key];

//            if (![_oldGiftKey isEqualToString:key]) {
//                [op.presentView removeShakeLabel];
//            }
//            _oldGiftKey = key;
            
            // 动画
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [op.presentView shakeNumberLabel];
                [op viewAnimation];
            }];
            
            // 取消延时删除礼物缓存
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(deleteGiftCache:) object:key];
            // 重新创建延时删除礼物缓存操作
            [self performSelector:@selector(deleteGiftCache:) withObject:key afterDelay:10];
            
            return;
        }
        // 没有操作,创建操作,并执行动画
        ZBGiftOperation *op = [ZBGiftOperation animationWithGiftIndex:giftIndex giftModel:giftModel finishedBlock:^(BOOL isFinished, NSInteger giftCount) {
            // 动画结束回调
            finishedBlock(isFinished, giftCount);
            
            // 删除操作缓存
            [self.oprationCache removeObjectForKey:@(giftModel.userID)];
            
            // 缓存礼物数
            [self.userInfoCache setObject:@(giftCount) forKey:key];
            
            
            // 多少秒收删除礼物缓存
            [self performSelector:@selector(deleteGiftCache:) withObject:key afterDelay:10];

        }];
        
        op.parentView = self.parentView;
        
        // 获取礼物数+1
        op.presentView.giftCount = [[self.userInfoCache objectForKey:key] integerValue] + 1;
//        op.giftModel = giftModel;
        op.presentView.headImageView.image = [UIImage imageNamed:giftModel.headName];
        op.presentView.giftImageView.image = [UIImage imageNamed:giftModel.giftImageName];
        op.presentView.nameLabel.text = giftModel.name;
        op.presentView.giftLabel.text = [NSString stringWithFormat:@"送了%@",giftModel.giftName];
//        if (![_oldGiftKey isEqualToString:key]) {
//            [op.presentView removeShakeLabel];
//        }
//        _oldGiftKey = key;
        // 将刚创建的操作放入操作缓存中(缓存中包括op上面所有设置的内容,以及其初始化中的内容)
        [self.oprationCache setObject:op forKey:@(giftModel.userID)];
        [self.userInfoCache setObject:@(op.presentView.giftCount) forKey:key];

        
        // 根据用户ID 控制显示的位置
        if (giftModel.userID % 2) {
            op.presentView.frame  = CGRectMake(-self.parentView.frame.size.width / 2, 300, self.parentView.frame.size.width / 2, 40);
            op.presentView.originFrame = op.presentView.frame;
            [self.queue1 addOperation:op];
        } else {
            op.presentView.frame  = CGRectMake(-self.parentView.frame.size.width / 2, 240, self.parentView.frame.size.width / 2, 40);
            op.presentView.originFrame = op.presentView.frame;
            [self.queue2 addOperation:op];
        }

    } else { // 没有用户缓存信息
        if ([self.oprationCache objectForKey:@(giftModel.userID)]) {
            // 操作缓存中存在, 则不创建操作  直接动画
            ZBGiftOperation *op = [self.oprationCache objectForKey:@(giftModel.userID)];
//            op.giftModel = giftModel;
            op.presentView.headImageView.image = [UIImage imageNamed:giftModel.headName];
            op.presentView.giftImageView.image = [UIImage imageNamed:giftModel.giftImageName];
            op.presentView.nameLabel.text = giftModel.name;
            op.presentView.giftLabel.text = [NSString stringWithFormat:@"送了%@",giftModel.giftName];
            op.presentView.giftCount = [[self.userInfoCache objectForKey:key] integerValue] + 1;
            [self.userInfoCache setObject:@(op.presentView.giftCount) forKey:key];
//
//            if (![_oldGiftKey isEqualToString:key]) {
//                [op.presentView removeShakeLabel];
//            }
//            _oldGiftKey = key;
            
            // 动画
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [op.presentView shakeNumberLabel];
                [op viewAnimation];
            }];
            
            // 取消延时删除礼物缓存
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(deleteGiftCache:) object:key];
            // 重新创建延时删除礼物缓存操作
            [self performSelector:@selector(deleteGiftCache:) withObject:key afterDelay:10];
            
            return;
        }
        // 没有操作,创建操作,并执行动画
        ZBGiftOperation *op = [ZBGiftOperation animationWithGiftIndex:giftIndex giftModel:giftModel finishedBlock:^(BOOL isFinished, NSInteger giftCount) {
            // 如果是礼物连送状态, 此block回调不会不会执行, 因为在内部将操作取消了
            // 动画结束回调
            finishedBlock(isFinished, giftCount);
            
            // 删除操作缓存
            [self.oprationCache removeObjectForKey:@(giftModel.userID)];
            
            // 缓存礼物数
            [self.userInfoCache setObject:@(giftCount) forKey:key];
            
            // 多少秒收后删除礼物缓存
            [self performSelector:@selector(deleteGiftCache:) withObject:key afterDelay:10];

        }];
        
        op.parentView = self.parentView;
        
        // 获取礼物数+1
        op.presentView.giftCount = 1;
//        op.giftModel = giftModel;
        op.presentView.headImageView.image = [UIImage imageNamed:giftModel.headName];
        op.presentView.giftImageView.image = [UIImage imageNamed:giftModel.giftImageName];
        op.presentView.nameLabel.text = giftModel.name;
        op.presentView.giftLabel.text = [NSString stringWithFormat:@"送了%@",giftModel.giftName];
//        if (![_oldGiftKey isEqualToString:key]) {
//            [op.presentView removeShakeLabel];
//        }
//        _oldGiftKey = key;
        // 将刚创建的操作放入操作缓存中(缓存中包括op上面所有设置的内容,以及其初始化中的内容)
        [self.oprationCache setObject:op forKey:@(giftModel.userID)];
        [self.userInfoCache setObject:@(op.presentView.giftCount) forKey:key];

        
        // 根据用户ID 控制显示的位置
        if (giftModel.userID % 2) {
            op.presentView.frame  = CGRectMake(-self.parentView.frame.size.width / 2, 300, self.parentView.frame.size.width / 2, 40);
            op.presentView.originFrame = op.presentView.frame;
            [self.queue1 addOperation:op];
        } else {
            op.presentView.frame  = CGRectMake(-self.parentView.frame.size.width / 2, 240, self.parentView.frame.size.width / 2, 40);
            op.presentView.originFrame = op.presentView.frame;
            [self.queue2 addOperation:op];
        }
    }
}

- (void)deleteGiftCache:(NSString *)key {
    NSLog(@"删除用户缓存");
    if (self.userInfoCache) {
        NSLog(@"++++%@",self.userInfoCache);
    }
    [self.userInfoCache removeObjectForKey:key];
    
}

- (NSOperationQueue *)queue1 {
    if (!_queue1) {
        _queue1 = [[NSOperationQueue alloc] init];
        _queue1.maxConcurrentOperationCount = 1;
    }
    return _queue1;
}

- (NSOperationQueue *)queue2 {
    if (!_queue2) {
        _queue2 = [[NSOperationQueue alloc] init];
        _queue2.maxConcurrentOperationCount = 1;
    }
    return _queue2;
}

- (NSMutableDictionary *)oprationCache {
    if (!_oprationCache) {
//        _oprationCache = [[NSCache alloc] init];
        _oprationCache = [NSMutableDictionary dictionary];
    }
    return _oprationCache;
}

- (NSMutableDictionary *)userInfoCache {
    if (!_userInfoCache) {
//        _userInfoCache = [[NSCache alloc] init];
        _userInfoCache = [NSMutableDictionary dictionary];
    }
    return _userInfoCache;
}












@end
